package com.example.ch4_1activity;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.service.controls.templates.ToggleRangeTemplate;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private Button btnJmp;
    private Button btnData;
    private ListView lvNames;


    //声明用于启动待返回的页面跳转功能的启动器
    private ActivityResultLauncher<Intent> laucher;
    private List<String> names;
    private ArrayAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


//        initData();
//        //实例化适配器对象
//        adapter = new ArrayAdapter(
//                this,
//                android.R.layout.simple_list_item_1,
//                names
//        );
//        //给ListView绑定适配器
//
//        //给ListView注册点击事件监听器
//        //获取launcher对象
//        laucher = registerForActivityResult(
//
//                new ActivityResultContracts.StartActivityForResult(),
//                new ActivityResultCallback<ActivityResult>() {
//                    @Override
//                    public void onActivityResult(ActivityResult result) {
//                        //判断由那个activity跳转回来
//                        switch (result.getResultCode()){
//                            case 1:
//                                //假设由第2个Activity返回的响应码是1
//                                //获取返回的intent
//                                Intent resultIntent = result.getData();
//                                //获取intent中携带的参数
//                                String name = resultIntent.getStringExtra("name");
//                                //更新数据源
//
//                                break;
//                        }
//                    }
//                }
//        );
        //获取控件对象
        getViews();

        //注册监听器
        SetListener();



    }

//    private void initData() {
//        names = new ArrayList<>();
//        names.add("张三");
//        names.add("lisi");
//        names.add("wangwu");
//
//    }

    private void SetListener() {
        OnCustomClickListener listener = new OnCustomClickListener();
        btnJmp.setOnClickListener(listener);
        btnData.setOnClickListener(listener);

//        lvNames.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//            @Override
//            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                //实例化intent对象
//                Intent intent = new Intent();
//                //设置跳转意图
//                intent.setClass(MainActivity.this,
//                        AnotherActivity2.class);
//                //封装数据
////                intent.putExtra("name",names.get(i));
//
//                //启动待反馈
//                laucher.launch(intent);
//            }
//        });


    }

    private void getViews() {
        btnJmp = findViewById(R.id.btn_jmp);
        btnData = findViewById(R.id.btn_data);
//        lvNames = findViewById(R.id.lv_name);
    }

    //自定义监听器类
    class OnCustomClickListener implements View.OnClickListener{

        @Override
        public void onClick(View v) {
            //判断事件源
            switch (v.getId()){
                case R.id.btn_jmp:
                    //跳转到anotheractivity
                    //1.实例化intent对象
                    Intent toAnotherIntent = new Intent();
                    //2.设置跳转目的地
                    toAnotherIntent.setClass(
                            MainActivity.this,
                            AnotherActivity2.class
                    );
                    //3.向intent中封装数据
                    toAnotherIntent.putExtra("msg","今天天气很凉爽");
                    //4.启动跳转
                    startActivity(toAnotherIntent);
                    break;
                case R.id.btn_data:
                    //携带复杂数据，Student对象进行逻辑跳转
                    //1.实例化intent对象，同时设置跳转意图
                    Intent toThridIntent = new Intent();
                    toThridIntent.setClass(
                            getApplicationContext(),
                            ThridActivity.class
                    );
                    //2.封装Student对象数据
//                    List<Student> stu = new ArrayList<>();
//                    Student stu1 = new Student("hanmeimei",20);
//                    Student stu2 = new Student("lilei",22);
                    Student stu3 = new Student("jiaozihao",21);
//                    stu.add(stu1);
//                    stu.add(stu2);
//                    stu.add(stu3);

                    //使用bundle对象封装数据
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("stu", stu3);


                    //将bundle对象太耐到intent中
                    toThridIntent.putExtra("obj",bundle);

                    //3.启动跳转
                    startActivity(toThridIntent);

                    break;

            }



        }
    }

}